package org.example;

public class Test1 {
    //leetcode 392 判断子序列 https://leetcode.cn/problems/is-subsequence/description/

    /**
     * dp
     * @param s
     * @param t
     * @return
     */
    public boolean isSubsequence(String s, String t) {
        char[] arr1 = s.toCharArray(), arr2 = t.toCharArray();
        int n1 = arr1.length, n2 = arr2.length;
        if (n1 == 0) return true;
        if (n2 == 0) return false;
        int[][] dp = new int[n1+1][n2+1];
        for (int i = 1; i <= n1; i++) {
            for (int j = 1; j <= n2; j++) {
                if (arr1[i-1] == arr2[j-1]) {
                    dp[i][j] = dp[i-1][j-1] + 1;
                }else {
                    dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        return dp[n1][n2] == n1;
    }

    /**
     * 双指针
     * @param s
     * @param t
     * @return
     */
    public boolean isSubsequence1(String s, String t) {
        char[] arr1 = s.toCharArray(), arr2 = t.toCharArray();
        int n1 = arr1.length, n2 = arr2.length;
        if (n1 == 0) return true;
        if (n2 == 0) return false;
        int index1 = 0, index2 = 0;
        while (index1 < n1 && index2 < n2) {
            if (arr1[index1] == arr2[index2]) {
                index1++;
            }
            index2++;
        }
        return index1 >= n1;
    }
}
